package com.hengpeng.itfin.port.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.itfin.common.MessageUtil;
import com.hengpeng.itfin.domain.annotation.RemoteService;
import com.hengpeng.itfin.domain.annotation.RemoteTransaction;
import com.hengpeng.itfin.exception.TransException;
import com.hengpeng.itfin.service.ServiceFactory;
import com.hengpeng.itfin.tools.SequenceGenerator;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.activity.CreateShareRedPacketRequest;
import com.hengpeng.itfinbase.message.activity.InvestBackRebateRequest;
import com.hengpeng.itfinbase.message.activity.InvestRedPacketRequest;
import com.hengpeng.itfinbase.message.activity.InvestSendMovieTicketRequest;
import com.hengpeng.itfinbase.message.activity.InviteSendRedPacketRequest;
import com.hengpeng.itfinbase.message.activity.RegisterSendRedPacketRequest;
import com.hengpeng.itfinbase.message.activity.UnfreeRedPacketRequest;
import com.hengpeng.itfinbase.message.activity.VipAddRateRequest;
import com.hengpeng.itfinbase.message.sequence.SequenceNoRequest;
import com.hengpeng.itfinbase.message.sequence.SequenceNoResponse;
import com.hengpeng.itfinbase.message.userInfo.AddMoneyRequest;
import com.hengpeng.itfinbase.message.userInfo.UserInfoResponse;

/**
 * 活动对外服务接口
 * 
 * @author weiyuan
 *
 */
@Service
@RemoteService
@Lazy
public class TransService {
    public final static String ERROR_MESSAGE = "系统异常";

    private static final Log logger = LogFactory.getLog(TransService.class);

    private static void errorLog(Throwable ex) {
        logger.error("活动服务调用异常。", ex);
    }

    @Autowired
    public ServiceFactory serviceFactory;

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.REGISTER_SEND_REDPACKET)
    public ResponseMessage registerSendRedPacket(RegisterSendRedPacketRequest request) {
        try {
            return serviceFactory.getRedPacketService().registerSendRedPacket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.INVITE_SEND_REDPACKET)
    public ResponseMessage inviteSendRedPacket(InviteSendRedPacketRequest request) {
        try {
            return serviceFactory.getRedPacketService().inviteSendRedPacket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.INVEST_SEND_REDPACKET)
    public ResponseMessage investSendRedPacket(InvestRedPacketRequest request) {
        try {
            return serviceFactory.getRedPacketService().investSendRedPacket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.INVEST_SEND_REBATE)
    public ResponseMessage investSendRebate(InvestBackRebateRequest request) {
        try {
            return serviceFactory.getRebateService().investSendRebate(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.UNFREE_REDPACKET)
    public ResponseMessage unFreeRedPacket(UnfreeRedPacketRequest request) {
        try {
            return serviceFactory.getRedPacketService().unFreeRedPacket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.CREATE_SHARE_REDPACKET)
    public ResponseMessage createShareRedPacket(CreateShareRedPacketRequest request) {
        try {
            return serviceFactory.getRedPacketService().createShareRedPacket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.INVEST_SEND_MOVIE_TICKET)
    public ResponseMessage investSendMovieTicket(InvestSendMovieTicketRequest request) {
        try {
            return serviceFactory.getRewadMovieService().investSendMovieTicket(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.ActivityTransCode.VIP_ADD_RATE)
    public ResponseMessage vipAddRate(VipAddRateRequest request) {
        try {
            return serviceFactory.getRewardAddRateService().vipAddRate(request);
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.OtherTransCode.SEQUENCE_NO)
    public SequenceNoResponse getSequenceNo(SequenceNoRequest request) {
        try {
            String sequenceNo = SequenceGenerator.createSequenceNo(
                    request.getPrefix(),
                    request.getSuffix(),
                    request.getLength(),
                    serviceFactory.getSequenceService().getSequence(request.getSequenceName(),
                            request.getSequenceCount(), request.getModMaxNum()));
            SequenceNoResponse response = new SequenceNoResponse();
            response.setSequenceNo(sequenceNo);
            return response;
        } catch (TransException ms) {
            errorLog(ms);
            return MessageUtil.getErrorResponseMessage(SequenceNoResponse.class, ms.getExceptionCode(),
                    ms.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(SequenceNoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

    @RemoteTransaction(transType = TransCodeConstants.OtherTransCode.ADD_MONEY)
    public ResponseMessage addMoney(AddMoneyRequest request) {
        try {
            serviceFactory.getAccountService().add(request.getUserId(), request.getType(), request.getMoney(),
                    request.getReferenceId(), request.getRemark());
            return new ResponseMessage();
        } catch (TransException tex) {
            errorLog(tex);
            return MessageUtil.getErrorResponseMessage(ResponseMessage.class, tex.getExceptionCode(),
                    tex.getExceptionMsg());
        } catch (Exception ex) {
            errorLog(ex);
            return MessageUtil.getErrorResponseMessage(UserInfoResponse.class,
                    Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, ERROR_MESSAGE);
        }
    }

}
